import { Component } from '@angular/core';
import { NavController,NavParams,AlertController } from 'ionic-angular';
import { HttpService } from '../../../providers/http.service';
import { CommonFunc } from '../../../providers/commonFunc';
import { Storage } from '@ionic/storage';
import { UsedControlInspection } from './bean/used-control-inspection.bean';
import { ElevatorUsedInspectionPage } from '../elevator-used-inspection/elevator-used-inspection';
import * as moment from 'moment';
import { ChangeDetectorRef } from '@angular/core';
/**
 * 单位安全管理检查
 */
@Component({
  templateUrl: 'security-manage.html'
})
export class SecurityManagePage {

  private usedControlInspection: UsedControlInspection= new UsedControlInspection();
  checkTimeFrom:any;
  isadd=true;
  isDetail=false;
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private httpService: HttpService,
    private navParams: NavParams,
    private commonFunc: CommonFunc,
    private storage: Storage,
    public changeDetectorRef:ChangeDetectorRef 
  ) {

    this.checkTimeFrom =  moment(new Date()).format("YYYYMMDDHHmmss");
    if(this.navParams.get('id')){
      this.isadd = false;
      this.isDetail = true;

      this.httpService.queryUsedControlInspect(this.navParams.get('id')).subscribe(data=>{
        if(data.status=='200'){
          this.usedControlInspection = data.data
        }
      });
    }
    
  }

  submit(){
      for(let key in this.usedControlInspection){
        if(key!='id'&&this.usedControlInspection[key]==null){
          const alert = this.alertCtrl.create({
            subTitle: '检查项填写不完整',
            buttons: ['确定']
          });
          alert.present();
          return;
        }
      }
      this.httpService.addUsedControl(this.usedControlInspection).subscribe((data1)=>{
            if(data1.status==200){
              this.navCtrl.push(ElevatorUsedInspectionPage,{checkTimeFrom:this.checkTimeFrom,usedControlId:data1.data.usedControlId});
            }else{
              const alert = this.alertCtrl.create({
                subTitle: '信息保存失败',
                buttons: ['确定']
              });
              alert.present();
            }
      })
  }


}
